<?php
    include_once('../inc/config.php');

    $accion = request('accion','');

    $sql = "select p.id,p.familia_id,p.titulo,f.nombre1
          from productos as p
          left join familias as f on p.familia_id=f.id
          where p.activo=1 order by f.nombre1 ASC, p.titulo ASC";
    $rs  = $db->Execute($sql);
    $Alojamientos = $rs->GetRows();

    foreach($Alojamientos as $clave=>$valor){

        $sql = "select fecha, sum(visualizacion) as visualizacion, sum(click) as click, sum(link) as  link, sum(contacto) as contacto 
                from visitas_unicas as vu
                where producto_id ='{$valor['id']}'
                group by fecha order by fecha ASC";
        $rs  = $db->Execute($sql);
        $Alojamientos[$clave]['stats'] = $rs->GetRows();
    }


    if($accion=='exportar'){

      $nombre_temporal = 'Estadisticas-'.date("Y-m-d-Hi").'.csv';
      $destino = ROOT.'/fer/'.$nombre_temporal;

      $fp = fopen("$destino", 'w');
      $renglon = "Categoria;Alojamiento;Periodo;EnListado;SuFicha;Contactos\n";
            
      fwrite($fp, $renglon);

      foreach($Alojamientos as $al){
        $t_visualiza = $t_click = $t_contacto = 0;
        foreach($al['stats'] as $st){
          $renglon = "{$al['nombre1']};{$al['titulo']};{$st['fecha']};{$st['visualizacion']};{$st['click']};{$st['contacto']}\n";
          fwrite($fp, $renglon);
            $t_visualiza = $t_visualiza + $st['visualizacion'];
            $t_click     = $t_click + $st['click'];
            $t_contacto  = $t_contacto + $st['contacto'];

        }
        $renglon = ";;TOTALES;$t_visualiza;$t_click;$t_contacto\n";
        fwrite($fp, $renglon);
        $renglon = ";;;;;\n";
        fwrite($fp, $renglon);

      }

      fclose($fp);
      $file = URL.'/fer/'.$nombre_temporal;
      $fecha = date("d-m-Y");
      header('Content-Encoding: UTF-8');
      header('Content-type: text/csv; charset=UTF-8');
      header("Content-Disposition: attachment; filename=$nombre_temporal");
      echo "\xEF\xBB\xBF"; // UTF-8 BOM
      readfile($file);
      unlink($destino);
      die();

    }




?>


<link href="<?php echo URL;?>/modules/bootstrap/css/bootstrap.css" rel="stylesheet" media="screen"  >
<link href="<?php echo URL;?>/modules/bootstrap/css/bootstrap-responsive.css" rel="stylesheet" media="screen"  >
<script src="<?php echo URL;?>/modules/bootstrap/js/bootstrap.min.js"></script>

<center>
  <a href='<?php echo URL;?>/fer/estadisticas.php?accion=exportar' target='blank' class='btn btn-info'>Exportar</a>
  <br>
<table style='width:500px;' class='table table-condensed'>
	<tbody>
  <?php 
      foreach($Alojamientos as $al){

        echo "<tr class='info'>
                <td style='text-align:center;'>{$al['nombre1']}</td>
                <td style='text-align:center;'>{$al['titulo']}</td>
              </tr>";

        echo "<tr><td colspan='2'>";      

        echo "<table class='table table-striped'>"; 
        echo "<thead>
                <tr>
                  <th style='text-align:center;'>Periodo</th>
                  <th style='text-align:center;'>En el Listado</th>      
                  <th style='text-align:center;'>Su Ficha</th>
                  <th style='text-align:center;'>Consultas Generadas</th>
                </tr>
              </thead>";

        echo "<tbody>";

        $t_visualiza = $t_click = $t_contacto = 0;

        foreach($al['stats'] as $st){

            $visualizacion = number_format($st['visualizacion'],0,',','.');
            $click         = number_format($st['click'],0,',','.');
            $contacto      = number_format($st['contacto'],0,',','.');

            echo "<tr>
                    <td style='text-align:center;'>{$st['fecha']}</td>
                    <td style='text-align:center;'>$visualizacion</td>
                    <td style='text-align:center;'>$click</td>
                    <td style='text-align:center;'>$contacto</td>
                  </tr>";

            $t_visualiza = $t_visualiza + $st['visualizacion'];
            $t_click     = $t_click + $st['click'];
            $t_contacto  = $t_contacto + $st['contacto'];

          } // endforeach stats

          echo "</tbody>";         

          echo "<tfoot>
                <tr>
                  <td style='text-align:right;'>Totales</td>
                  <td style='text-align:center;'>".number_format($t_visualiza,0,',','.')."</td>
                  <td style='text-align:center;'>".number_format($t_click,0,',','.')."</td>
                  <td style='text-align:center;'>".number_format($t_contacto,0,',','.')."</td>
                </tr>
              </tfoot>";
          echo "</table>";

          echo "</td></tr>";

      }
	?>
  </tbody>
</table>
</center>